import csv
import json
from pprint import pprint
import requests
import datetime
import os

# new_dir = input('你要保存的文件夹名：')
now = datetime.datetime.now()
new_dir = '%s%02d%02d%02d%02d'%(now.year , now.month , now.day,now.hour,now.minute)
os.mkdir(new_dir)

site_list = [{'947': '香山公园'},
             {'946': '奥林匹克森林公园'},
             {'945': '世界葡萄博览中心景区'},
             {'944': '松山森林旅游区'},
             {'943': '野鸭湖国家湿地公园'},
             {'942': '延庆百里山水画廊景区'},
             {'941': '张裕爱菲堡国际酒庄'},
             {'940': '桃源仙谷自然风景区'},
             {'939': '黑龙潭旅游区'},
             {'938': '司马台长城景区'},
             {'937': '黄花水长城旅游区'},
             {'936': '青龙峡旅游度假区'},
             {'935': '京东大峡谷旅游区'},
             {'934': '丫髻山道教文化名胜区'},
             {'933': '京东石林峡分景区'},
             {'932': '金海湖风景区'},
             {'931': '平谷青龙山旅游区'},
             {'930': '绿野晴川野生动物园'},
             {'929': '银山塔林风景区'},
             {'928': '温都水城'},
             {'927': '小汤山现代农业示范园'},
             {'926': '龙脉温泉度假村'},
             {'925': '中国航空博物馆'},

             {'703': '八达岭'},
             {'704': '八大处'},
             {'706': '颐和园'},
             {'707': '故宫博物院'},
             {'708': '世界公园'},
             {'751': '天坛公园'},
             {'753': '红螺寺'},
             {'756': '龙庆峡旅游区'},
             {'758': '北京动物园'},
             {'761': '石景山游乐园'},
             {'762': '慕田峪长城'},
             {'763': '景山公园'},
             {'764': '中山公园'},
             {'767': '北京海洋馆'},
             {'769': '玉渊潭公园'},
             {'770': '陶然亭公园'},
             {'773': '紫竹院公园'},
             {'777': '恭王府'},
             {'778': '北京欢乐谷'},
             {'779': '十三陵'},
             {'780': '北宫国家森林公园'},
             {'783': '北海公园'},
             {'805': '凤凰岭自然风景区'},
             {'806': '朝阳公园'},
             {'807': '龙潭公园'},
             {'808': '圆明园遗址公园'},
             {'810': '地坛公园'},
             {'815': '什刹海风景区'},
             {'817': '云居寺'},
             {'818': '潭柘寺'},
             {'819': '八达岭水关长城景区'},
             {'820': '居庸关长城'},
             {'821': '世界花卉大观园'},
             {'822': '雁栖湖旅游区'},
             {'901': '孔庙国子监'},
             {'902': '北京规划展览馆'},
             {'903': '明城墙遗址公园'},
             {'904': '首都博物馆'},
             {'905': '北京天文馆'},
             {'907': '中国紫檀博物馆'},
             {'908': '元大都城垣遗址公园'},
             {'909': '蟹岛绿色生态农庄'},
             {'910': '北京市植物园'},
             {'911': '中央广播电视塔'},
             {'912': '南宫旅游景区'},
             {'913': '中国人民抗日战争纪念馆'},
             {'914': '北京汽车博物馆'},
             {'915': '北京园博园'},
             {'916': '戒台寺'},
             {'917': '石花洞'},
             {'918': '周口店遗址博物馆'},
             {'919': '北京十渡风景名胜区'},
             {'920': '圣莲山旅游度假区'},
             {'921': '通州大运河森林旅游区'},
             {'922': '北京韩美林艺术馆'},
             {'923': '北京奥林匹克水上公园'},
             {'924': '北京顺义国际鲜花港'},
             # 景点添加处 ...
             # 注意标点状态半角
             ]
print(len(site_list))
count = 0
for item in site_list:
    for key, value in item.items():
        url = "http://s.visitbeijing.com.cn/index.php?c=flow&catid=26&sid=%s" % key
    response = requests.get(url)
    count += 1
    print("第%s个" % count + str(response.status_code))
    content = response.content.decode()[1:-1]
    data = json.loads(content)["flow"]
    # pprint(data)
    with open('%s/%s.csv' % (new_dir, value), 'w', encoding='utf-8', newline='') as f:
        csv_writer = csv.writer(f)
        csv_writer.writerow(['时间', '人流量/万', '景区舒适度'])
        for tmp in data:
            csv_writer.writerow(tmp.values())
            # break
